package model;

import dao.DaoException;
import dao.DaoFactory;
import dao.MeetingDao;
import java.util.HashMap;
import java.io.Serializable;
import java.sql.Timestamp;

/**
 * 
 * This class is Meeting model class
 * 
 * The meeting class represents a meeting table in the database. 
 * Each meeting belongs to a user and contains its attendees 
 * separated by commas. 
 * 
 * @author Buthaina Haldosary, Sukhjinder K. Narula
 * @since May 2013
 * @version $Revision: 1.0 $
 */

public class Meeting implements Serializable{

	/**
	 *  Class properties
	 */
	private static final long serialVersionUID = 1L;
	transient private MeetingDao dao;
	transient private int id;
	private int userId;
	private String owner;
	private int revisionId;
	private String topic;
	private String location;
	private Timestamp start;
	private Timestamp end;
	private String attendees;
	private String description;
	
	
	// copy fields used for editing a meeting locally
	private String c_topic;
	private String c_location;
	private Timestamp c_start;
	private Timestamp c_end;
	private String c_attendees;
	private String c_description;

	/**
	 * Constructor
	 * @param userId int
	 * @param owner String
	 * @param revisionId int
	 * @param topic String
	 * @param location String
	 * @param start Timestamp
	 * @param end Timestamp
	 * @param attendees String
	 * @param description String
	 */
	
	public Meeting()
	{
		userId=-1;
		owner="";
		id=-1;
	}
	public Meeting(int userId, String owner, int revisionId, String topic,
			String location,
			Timestamp start, 
			Timestamp end,
			String attendees,
			String description) {
		this.id = -1;
		this.userId = userId;
		this.owner = owner;
		this.revisionId = revisionId;
		this.topic = topic;
		this.c_topic=topic;
		this.location = location;
		this.c_location=location;
		this.start = start;
		this.c_start=start;
		this.end = end;
		this.c_end=end;
		this.attendees = attendees;
		this.c_attendees=attendees;
		this.description = description;
		this.c_description=description;
		this.dao = DaoFactory.getMeetingDao();
	}

	/**
	 * Get the meeting id
	
	 * @return int: meeting id */
	public int getId() {
		return id;
	}
	
	/**
	 * Sets the meeting id
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Gets a user id	
	 * @return userId: numeric unique value of the user id 
	 */
	public int getUserId() {
		return userId;
	}

	/**
	 * Sets the user id.
	
	 * @param userId int
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}
	

	/**
	 * Get the owner name
	 * @return owner: name of the user who created the meeting
	 */
	public String getOwner() {
		return owner;
	}

	/**
	 * Set the owner of the meeting
	 * @param owner name
	 */
	public void setOwner(String owner) {
		this.owner = owner;
	}

	/**
	 * Get the revisionId
	 * @return revisionId: revision number of the meeting
	 */
	public int getRevisionId() {
		return revisionId;
	}

	/**
	 * Set the Revision id of the meeting
	 * Everytime the meeting is saved on server, the revision id is increamented 
	 * @param revisionId
	 */
	public void setRevisionId(int revisionId) {
		this.revisionId = revisionId;
	}

	/**
	 * Get the meeting topic
	
	 * @return string: meeting topic */
	public String getTopic() {
		return topic;
	}

	/**
	 * Sets the meeting topic
	
	 * @param topic String
	 */
	public void setTopic(String topic) {
		this.topic = topic;
	}

	/**
	 * Get the meeting location
	
	 * @return string: meeting location */
	public String getLocation() {
		return location;
	}

	/**
	 * Sets the meeting location
	
	 * @param location String
	 */
	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * Gets the meeting start date and time
	
	 * @return Timestamp: meeting start date and time */
	public Timestamp getStart() {
		return start;
	}

	/**
	 * Sets the meeting start date and time
	
	 * @param start Timestamp
	 */
	public void setStart(Timestamp start) {
		this.start = start;
	}

	/**
	 * Gets the meeting end date and time
	
	 * @return Timestamp: meeting end date and time */
	public Timestamp getEnd() {
		return end;
	}

	/**
	 * Sets the meeting end date and time
	
	 * @param end Timestamp
	 */
	public void setEnd(Timestamp end) {
		this.end = end;
	}

	/**
	 * Get the meeting attendees separated by commas
	
	 * @return string: meeting attendees */
	public String getAttendees() {
		return attendees;
	}

	/**
	 * Sets the meeting attendees separated by commas
	
	 * @param attendees String
	 */
	public void setAttendees(String attendees) {
		this.attendees = attendees;
	}
	
	/**
	 * Get the meeting description
	
	 * @return string: meeting description */
	public String getDescription() {
		return description;
	}

	/**
	 * Sets the meeting description
	
	 * @param description String
	 */
	public void setDescription(String description) {
		this.description = description;
	}	
	
	/**
	 * Saves the current state of the meeting into the database
	
	 * @throws ModelException  */
	public void save() throws ModelException{
		
		if (id == -1){
			try {
				id = dao.create(getTransferObject());
			} catch (DaoException e) {
				throw new ModelException(e.getMessage());
			}
		} else {
			try {
				dao.update(getTransferObject());
			} catch (DaoException e) {
				throw new ModelException(e.getMessage());
			}
		}
	}
	
	/**
	 * Creates string hash map of properties
	
	 * @return a String representation of the properties. */
	public HashMap<String, String> getTransferObject(){		
		HashMap<String, String> toReturn = new HashMap<String, String>();
		toReturn.put("Id", String.valueOf(id));
		toReturn.put("UserId", String.valueOf(userId));
		toReturn.put("Topic", topic);
		toReturn.put("C_Topic", c_topic);
		toReturn.put("Location", location);
		toReturn.put("C_Location", c_location);
		toReturn.put("Start", String.valueOf(start));
		toReturn.put("C_Start", String.valueOf(c_start));
		toReturn.put("End", String.valueOf(end));
		toReturn.put("C_End", String.valueOf(c_end));
		toReturn.put("Attendees", attendees);
		toReturn.put("C_Attendees", c_attendees);
		toReturn.put("Description", description);
		toReturn.put("C_Description", c_description);
		return toReturn;
	}

	/**
	 * Deletes the meeting from the database
	
	 * @throws ModelException  */
	public void delete() throws ModelException {
		try {
			dao.delete(id);
		} catch (DaoException e) {
			throw new ModelException(e.getMessage());
		}
	}
	/**
	 * This method checks if two meetings have same timestamp values
	 * @param mt
	
	 * @return boolean indicator */
	public boolean hasSameDates(Meeting mt) {
		if((this.getStart().compareTo(mt.getStart())==0)&&(this.getEnd().compareTo(mt.getEnd())==0))
		return true;
		return false;
	}
	/**
	 * This method checks if two meeting's timestamps overlap
	 * @param mt
	
	 * @return boolean indicator */
	public boolean datesOverLap(Meeting mt) {
		if(this.getStart().after(mt.getStart())&&(this.getEnd().before(mt.getEnd())))
		return true;
		return false;	
	}

	/**
	 * This method returns the copy of the topic
	 * @return
	 */
	public String getC_topic() {
		return c_topic;
	}

	/**
	 * This method sets the copy of the topic
	 * @param c_topic
	 */
	public void setC_topic(String c_topic) {
		this.c_topic = c_topic;
	}
	/**
	 * This method gets the copy of the location
	 * @return
	 */
	public String getC_location() {
		return c_location;
	}
	/**
	 * This method sets the copy of the location
	 * @param c_location
	 */
	public void setC_location(String c_location) {
		this.c_location = c_location;
	}
	/**
	 * This method gets the copy of the start time
	 * @return
	 */
	public Timestamp getC_start() {
		return c_start;
	}
	/**
	 * This method sets the copy of the start time
	 * @param c_start
	 */
	public void setC_start(Timestamp c_start) {
		this.c_start = c_start;
	}
	/**
	 * This method gets the copy of the end time
	 * @return
	 */
	public Timestamp getC_end() {
		return c_end;
	}
	/**
	 * This method sets the copy of the end time
	 * @param c_end
	 */
	public void setC_end(Timestamp c_end) {
		this.c_end = c_end;
	}
	/**
	 * This method gets the copy of the attendees
	 * @return
	 */
	public String getC_attendees() {
		return c_attendees;
	}
	/**
	 * This method sets the copy of the attendees
	 * @param c_attendees
	 */
	public void setC_attendees(String c_attendees) {
		this.c_attendees = c_attendees;
	}
	/**
	 * This method gets the copy of the description
	 * @return
	 */
	public String getC_description() {
		return c_description;
	}
	/**
	 * This method sets the copy of the description
	 * @param c_description
	 */
	public void setC_description(String c_description) {
		this.c_description = c_description;
	}

}
